/**
 * 
 */
package at.ac.lbg.media.vis.framework.webservice;

import java.util.List;

import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;

import at.ac.lbg.media.vis.framework.model.Artwork;
import at.ac.lbg.media.vis.framework.model.Category;
import at.ac.lbg.media.vis.framework.model.ObjectFactory;
import at.ac.lbg.media.vis.framework.model.ThemeRequest;
import at.ac.lbg.media.vis.framework.service.IThemeService;

/**
 * @author Evelyn Münster
 *
 */
public class ThemeWebService extends AbstractMarshallingPayloadEndpoint {
	
	private IThemeService themeService;
	private ObjectFactory objectFactory;



	@Override
	protected Object invokeInternal(Object requestObject) throws Exception {
		String command = (String) requestObject;
		if (command != null) {
			if (command.equals("leafCategories")) {
				return translateCategories(themeService.getKeywords());
			}
		}
		List<Artwork> allArtworks = themeService.getAllArtworks();
		return translateArtworks(allArtworks);
		
	}
	
	private ThemeRequest translateCategories(List<Category> categories) {
		ThemeRequest req = objectFactory.createThemeRequest();
		for (Category c : categories) {
			req.setCategory(objectFactory.createCategoryType(c));
		}
		return req;
	}
	
	private ThemeRequest translateArtworks(List<Artwork> artworks) {
		ThemeRequest req = objectFactory.createThemeRequest();
		for (Artwork a : artworks) {
			req.setArtwork(objectFactory.createArtworkType(a));
		}
		return req;
	}
	
	/**
	 * Dependency Injection
	 * @param themeService
	 */
	public void setThemeService(IThemeService themeService) {
		this.themeService = themeService;
	}
	
	/**
	 * Dependency Injection
	 * @param objectFactory
	 */
	public void setObjectFactory(ObjectFactory objectFactory) {
		this.objectFactory = objectFactory;
	}
	

}
